package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.annotation.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    //http://localhost:8500/api/album/trackInfo/uploadTrack
    @Operation(summary = "上传声音")
    @PostMapping("/uploadTrack")
    public Result uploadTrack(MultipartFile file) {
        Map<String, Object> map = trackInfoService.uploadTrack(file);
        return Result.ok(map);
    }

    @PostMapping("/saveTrackInfo")
    @Operation(summary = "保存声音信息")
    @TingshuLogin
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    //Request URL: http://192.168.200.1:8500/api/album/trackInfo/findUserTrackPage/1/10
    @PostMapping("/findUserTrackPage/{pn}/{pz}")
    @Operation(summary = "查询登录用户的声音分页展示")
    @TingshuLogin
    public Result findUserTrackPage(@PathVariable(value = "pn") Long pn,
                                    @PathVariable(value = "pz") Long pz,
                                    @RequestBody TrackInfoQuery trackInfoQuery) {
        Page<TrackListVo> trackListVoPage = new Page<>(pn, pz);
        trackInfoQuery.setUserId(AuthContextHolder.getUserId());
        trackListVoPage = trackInfoService.findUserTrackPage(trackListVoPage, trackInfoQuery);
        return Result.ok(trackListVoPage);
    }

    @GetMapping(value = "/getTrackInfo/{trackId}")
    @Operation(summary = "回显声音")
    public Result getTrackInfo(@PathVariable(value = "trackId") Long trackId) {
        TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
        return Result.ok(trackInfo);
    }

    // Request URL: http://192.168.200.1:8500/api/album/trackInfo/updateTrackInfo/51944
    @PutMapping("/updateTrackInfo/{trackId}")
    @Operation(summary = "修改声音")
    @TingshuLogin   // 校验用户是否登录
    public Result updateTrackInfo(@RequestBody TrackInfoVo trackInfoVo,
                                  @PathVariable(value = "trackId") Long trackId) {
        trackInfoService.updateTrackInfo(trackInfoVo,trackId);
        return Result.ok();
    }


    //http://localhost:8500/api/album/trackInfo/removeTrackInfo/51948
    @DeleteMapping("/removeTrackInfo/{trackId}")
    @Operation(summary = "删除声音")
    @TingshuLogin   // 校验用户是否登录
    public Result removeTrackInfo(@PathVariable(value = "trackId") Long trackId) {
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }

    //Request URL: http://localhost:8500/api/album/trackInfo/findAlbumTrackPage/214/1/10
    @GetMapping("/findAlbumTrackPage/{albumId}/{pn}/{pz}")
    @Operation(summary = "根据专辑id查询专辑下的声音列表")
    // 问题一：要展示专辑下对应的声音是否要付费（通过图标展示）
    // 如果未来有付费图标，那么该声音就需要付费
    // 如果未来没有付费图标，那么该声音就免费
    // 问题二：查询该接口是否要求用户一定登录。
    // 1.可以不登录，直接进来看
    // 2.可以登录，在来看
    @TingshuLogin(required = false)
    public Result findAlbumTrackPage(@PathVariable(value = "albumId") Long albumId,
                                     @PathVariable(value = "pn") Long pn,
                                     @PathVariable(value = "pz") Long pz) {
        IPage<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pn, pz);
        albumTrackListVoPage=trackInfoService.findAlbumTrackPage(albumId,albumTrackListVoPage);
        return Result.ok(albumTrackListVoPage);
    }

    // Request URL: http://192.168.200.1:8500/api/album/trackInfo/getTrackStatVo/51946
    @Operation(summary = "查询声音的各个维度值")
    @GetMapping("/getTrackStatVo/{trackId}")
    public Result getTrackStatVo(@PathVariable(value = "trackId") Long trackId) {

        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }

    //Request URL: http://localhost:8500/api/album/trackInfo/findUserTrackPaidList/49930
    @Operation(summary = "分集展示要购买的声音列表")
    @GetMapping("/findUserTrackPaidList/{currentTrackId}")
    @TingshuLogin
    public Result findUserTrackPaidList(@PathVariable(value = "currentTrackId") Long currentTrackId) {
        List<Map<String,Object>> trackPaidMapList = trackInfoService.findUserTrackPaidList(currentTrackId);
        return Result.ok(trackPaidMapList );
    }
}

